﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Nomad.WebSync.Extensions;

namespace Nomad.WebSync.Pocket {
    public partial class configForm : Form {
        private Settings settings;

        public configForm() {
            InitializeComponent();            
        }        

        private void loadSettings() {
            this.settings = new Settings();
            
            serviceTextBox.Text = this.settings.Service;
            usernameTextBox.Text = this.settings.Username;
            passwordTextBox.Text = this.settings.Password;

            contactsCheckBox.Checked = this.settings.SyncContacts;
            calendarCheckBox.Checked = this.settings.SyncCalendar;
            tasksCheckBox.Checked = this.settings.SyncTasks;
            notesCheckBox.Checked = this.settings.SyncNotes;
        }

        private void applyAccountSettings() {            
            this.settings.Service = serviceTextBox.Text;
            this.settings.Username = usernameTextBox.Text;
            this.settings.Password = passwordTextBox.Text;
            this.settings.Write();

            Program.MainForm.LoadSettings();

            MessageBox.Show("New settings updated!");
        }
        private void applyTaskSettings() {
            this.settings.SyncCalendar = calendarCheckBox.Checked;
            this.settings.SyncContacts = contactsCheckBox.Checked;
            this.settings.SyncNotes = notesCheckBox.Checked;
            this.settings.SyncTasks = tasksCheckBox.Checked;
            this.settings.Write();

            Program.MainForm.LoadSettings();

            MessageBox.Show("New settings updated!");
        }

        private void cancelAccountSettings() {
            serviceTextBox.Text = this.settings.Service;
            usernameTextBox.Text = this.settings.Username;
            passwordTextBox.Text = this.settings.Password;
        }
        private void cancelTaskSettings() {
            contactsCheckBox.Checked = this.settings.SyncContacts;
            calendarCheckBox.Checked = this.settings.SyncCalendar;
            tasksCheckBox.Checked = this.settings.SyncTasks;
            notesCheckBox.Checked = this.settings.SyncNotes;
        }

        private void configForm_Load(object sender, EventArgs e) {
            try {
                this.loadSettings();
            } catch (System.Exception ex) {
                MessageBox.Show(ex.SoapMessage());
            }            
        }
        private void applyTasksbutton_Click(object sender, EventArgs e) {
            try {
                this.applyTaskSettings();
            } catch (System.Exception ex) {
                MessageBox.Show(ex.SoapMessage());
            }
        }
        private void cancelTaskButton_Click(object sender, EventArgs e) {
            try {
                this.cancelTaskSettings();
            } catch (System.Exception ex) {
                MessageBox.Show(ex.SoapMessage());
            }
        }
        private void applyUserAccountButton_Click(object sender, EventArgs e) {
            try {
                this.applyAccountSettings();
            } catch (System.Exception ex) {
                MessageBox.Show(ex.SoapMessage());
            }
        }
        private void cancelUserAccountButton_Click(object sender, EventArgs e) {
            try {
                this.cancelAccountSettings();
            } catch (System.Exception ex) {
                MessageBox.Show(ex.SoapMessage());
            }
        }
    }
}